package com.txc.common.global.advice;

import com.txc.common.global.exception.CustomException;
import com.txc.common.global.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @ClassName: ExceptionAdvice
 * @Author: TXC
 * @Date: 2022-07-30 10:05
 **/
@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result<?> exception(Exception e) {
        log.error("全局异常信息 exception={}", e.getMessage(), e);
        return Result.fail(500, e.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public Result<?> customExceptionHandler(CustomException e) {
        log.error("用户异常 exception={}", e.getMessage(), e);
        return Result.fail(e.getCode(), e.getMessage());
    }

}
